/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var ide = "hola jejje";

function buscarRed(){
    if($("#buscar").val().length==0){
        return;
    }
    $.ajax(
    {
        type: "GET",
        url: "buscarRed",   
        async: false,
        dataType: "text",
        data: "buscar="+$("#buscar").val(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#body-content").html(transport);
            $('input[id="btnVer"]').click(verRed);
            $('input[id="btnEliminar"]').click(eliminarRed);
            $('input[id="btnModificar"]').click(modificarRed);
            $('input[id="btnVerRed"]').click(verRed);
            $('#btnAgregar').click(agregarRed);
            $('#btnBuscar').click(buscarRed);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function modificarRedA(){
    if(!validaAltaRed()){
        alert("Campos invalidos");
        return false;
    }
        
    $.ajax(
    {
        type: "POST",
        url: "modificarRed",
        async: false,
        dataType: "text",
        data: $('#modficarRedF').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);
            $("#body-content").html("");
            $('#menuRed').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
    $('#menuRed').trigger('click');
    $('#menuRed').trigger('click');
    return false;                        
}

function modificarRed(){    
    $("#tablaRed").hide();
    
    $.ajax(
    {
        type: "GET",
        url: "gestionModificarRedPre",
        async: false,
        dataType: "text",
        data: "idRed="+$(this).closest('tr').attr("id"),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verRed").html(transport);
            $(".error-inner").hide();
            $("#verRed").show();
            $("#tablaRed").hide();
            $('#btnModificarRed').click(modificarRedA);
            $("#btnRegresar").click(mostrarRed);            
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function eliminarRed(){ 
    id = $(this).closest('tr').attr("id");
    if(!confirm("Seguro que desea eliminar la Red: "+ id))
        return;   
    $.ajax(
    {
        type: "GET",
        url: "eliminarRed",
        async: false,
        dataType: "text",
        data: "idRed="+ id,
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport); 
            $("#"+id).remove()
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function mostrarRed(){
    $("#verRed").hide();   
    $("#tablaRed").show();
}

function agregarRed(){  
    $("#tablaRed").hide();
    
    $.ajax(
    {
        type: "GET",
        url: "GestionRed/gestionAgregarRed.jsp",
        async: false,
        dataType: "text",
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verRed").html(transport);
            $(".error-inner").hide();
            $("#verRed").show();
            $("#tablaRed").hide();
            $('#btnAltaRed').click(altaRed);
            $("#btnRegresar").click(mostrarRed);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function altaRed(){
    if(!validaAltaRed())
        return false;
       
    $.ajax(
    {
        type: "POST",
        url: "altaRed",
        async: false,
        dataType: "text",
        data: $('#altaRed').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);
            $("#body-content").html("");
            $('#menuRed').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
    $('#menuRed').trigger('click');
    $('#menuRed').trigger('click');
    return false;
}

function validaAltaRed(){
    $('.error-inner').hide();
    esValido = true;
    if($('#nombrered').val().length<3){
        $("#enombrered").show();
        esValido = false;
    }    
    return esValido;
}

function validaAltaEquipoRed(){
    $('.error-inner').hide();
    esValido = true;
    if($('#ip').val().length<6){
        $("#eip").show();
        esValido = false;
    }    
    if($('#mac').val().length<6){
        $("#emac").show();
        esValido = false;
    }    
    return esValido;
}

function verRed(){ 
    ide = $(this).closest('tr').attr("id")
    $.ajax(
    {
        type: "GET",
        url: "consultaEquiposRed",
        async: false,
        dataType: "text",
        data: "idRed="+ ide,       
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verRed").html(transport);
            $("#verRed").show();
            $("#tablaRed").hide();
            $("#btnRegresar").click(mostrarRed);
            $('input[id="btnModificarRed"]').click(modificarEquipoRedPre);
            $('input[id="btnEliminarRed"]').click(eliminarEquipoRed);
            $("#btnAgregar").click(agregarEquipoRed);
				
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });        
}

function verRedE(){      
    $.ajax(
    {
        type: "GET",
        url: "consultaEquiposRed",
        async: false,
        dataType: "text",
        data: "idRed="+ ide,       
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verRed").html(transport);
            $("#verRed").show();
            $("#tablaRed").hide();
            $("#btnRegresar").click(mostrarRed);
            $('input[id="btnModificarRed"]').click(modificarEquipoRedPre);
            $('input[id="btnEliminarRed"]').click(eliminarEquipoRed);
            $("#btnAgregar").click(agregarEquipoRed);
				
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });        
}

function modificarEquipoRedPre(){   
    $.ajax(
    {
        type: "POST",
        url: "modificarEquipoRedPre",
        async: false,
        dataType: "text",
        data: "idDetalleRed="+$(this).closest('tr').attr("id"),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {          
            $("#verRed").html(transport);
            $(".error-inner").hide();
            $("#verRed").show();
            $("#tablaRed").hide();
            $('#btnModificarEquipoRedF').click(modificarEquipoRed);
            $("#btnRegresarE").click(verRedE);                     
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });   
    return false;                        
}

function modificarEquipoRed(){        
    if(!validaAltaEquipoRed()){
        alert("Campos invalidos");
        return false;
    }
        
    $.ajax(
    {
        type: "POST",
        url: "modficarEquipoRed",
        async: false,
        dataType: "text",
        data: $('#modficarEquipoRed').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);
            $('#btnRegresarE').trigger('click');                                                                                                                   
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }        
    }); 
    $('#btnRegresarE').trigger('click'); 
    $('#btnRegresarE').trigger('click'); 
    return false;                        
}

function eliminarEquipoRed(){ 
    id = $(this).closest('tr').attr("id");
    if(!confirm("Seguro que desea eliminar el detalle de red : "+ id))
        return;   
    $.ajax(
    {
        type: "GET",
        url: "eliminarEquipoRed",
        async: false,
        dataType: "text",
        data: "idDetalleRed="+ id,
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport); 
            $("#"+id).remove()
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function agregarEquipoRed(){    
    $("#tablaRed").hide();    
    $.ajax(
    {
        type: "GET",
        url: "gestionAgregarEquipoRedPre",
        async: false,
        dataType: "text",
        data: "idRed=" + ide,
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verRed").html(transport);
            $(".error-inner").hide();
            $("#verRed").show();
            $("#tablaRed").hide();
            $('#btnAltaEquipoRed').click(altaEquipoRed);
            $("#btnRegresarA").click(verRedE);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });    
}

function altaEquipoRed(){
    if(!validaAltaEquipoRed())
        return false;
       
    $.ajax(
    {
        type: "POST",
        url: "altaEquipoRed",
        async: false,
        dataType: "text",
        data: $('#altaEquipoRed').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);           
            $('#btnRegresarA').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
    $('#btnRegresarA').trigger('click');
    $('#btnRegresarA').trigger('click');
    return false;
}